package com.michael.demos.springboot.openapi.controller;

import com.michael.demos.springboot.openapi.entity.AppUser;
import com.michael.demos.springboot.openapi.exception.APIException;
import com.michael.demos.springboot.openapi.service.ErrorCode;
import com.michael.demos.springboot.openapi.service.OpenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 类功能描述:
 * <pre>
 *   统一的请求入口
 *
 *  1. /open-api/gen
 *  2. /open-api/getToken?appKey=&appSecret=
 *  3. /open-api/a?name=abc&timestamp=&nonce=&token=&sign=
 *
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/11/26 16:12
 */
@Slf4j
@RestController
@RequestMapping("/open-api")
public class OpenApiController {

    @Autowired
    private OpenApiService openApiService;

    /** 生成用户信息 */
    @GetMapping("/gen")
    public Map<String, Object> gen() {

        Map<String, Object> resultMap = initResultMap();
        try {

            AppUser user = openApiService.genOne();
            resultMap.put("user", user);

        } catch (APIException e) {
            setErrorMsg(resultMap, e);
        }
        return resultMap;
    }

    /** 获取 Token */
    @GetMapping("/getToken")
    public Map<String, Object> getToken(@RequestParam String appKey, @RequestParam String appSecret) {

        Map<String, Object> resultMap = initResultMap();
        try {

            String token = openApiService.getToken(appKey, appSecret);
            resultMap.put("token", token);

        } catch (APIException e) {
            setErrorMsg(resultMap, e);
        }
        return resultMap;
    }

    /** 调用接口A */
    @GetMapping("/a")
    public Map<String, Object> doA(@RequestParam String name, HttpServletRequest request) {

        Map<String, String[]> paramMapTemp = request.getParameterMap();
        //  请求参数转为 Map<String,String>
        Map<String, String> paramMap = new HashMap<>();
        paramMapTemp.forEach((k, v) -> paramMap.put(k, v[0]));

        Map<String, Object> resultMap = initResultMap();

        try {
            if (openApiService.checkOk(paramMap)) {

                String result = openApiService.doA(name);
                resultMap.put("result", result);
            }

        } catch (APIException e) {
            setErrorMsg(resultMap, e);
        }
        return resultMap;
    }

    private Map<String, Object> initResultMap() {
        Map<String, Object> resultMap = new HashMap<>(16);
        resultMap.put("code", 0);
        return resultMap;
    }

    private void setErrorMsg(Map<String, Object> resultMap, APIException e) {
        ErrorCode errorCode = e.getErrorCode();
        resultMap.put("code", errorCode.getCode());
        resultMap.put("msg", errorCode.getMsg());
    }
}
